/*
 * @Author: sean
 * @Date:   2019-12-18 10:45:18
 * @Last Modified by:   sean
 * @Last Modified time: 2020-01-02 10:56:13
 */

const state = {
    main: {
        /**
         * 资源管理器
         * @type {Array}
         */
        resources: [],
        /**
         * 控制台
         * @type {Object}
         */
        controls: {},
        /**
         * 操作台
         * @type {Array}
         */
        libraries: [],
        /**
         * 属性台
         * @type {Array}
         */
        attributes: [],
        /**
         * 图层台
         * @type {Array}
         */
        layers: []
    },
    config: {

    }
}

const getters = {
    models: function(state) {
        let data = {};
        if (state.main.resources.length > 0) {
            let value = state.main.resources[0];
            if (value.hasOwnProperty('data') && value.data.hasOwnProperty('models')) {
                data.present = value.data.models;
            }
        }
        if (state.main.libraries.length > 0) {
            let value = state.main.libraries[0];
            if (value.hasOwnProperty('data')) {
                data.target = value.data;
            }
        }
        return data
    },
    gather: function(state) {
        let data = {};
        if (state.main.libraries.length > 0) {
            let value = state.main.libraries[0];
            if (value.hasOwnProperty('data')) {
                data.present = value.data;
            }
        }
        if (state.main.libraries.length > 0) {
            let value = state.main.libraries[1];
            if (value.hasOwnProperty('data')) {
                data.target = value.data;
            }
        }
        return data
    },
    action: function(state) {
        let data = {};
        if (state.main.libraries.length > 0) {
            let value = state.main.libraries[1];
            if (value.hasOwnProperty('data')) {
                data.present = value.data;
            }
        }
        if (state.main.libraries.length > 0) {
            let value = state.main.libraries[2];
            if (value.hasOwnProperty('data')) {
                data.target = value.data;
            }
        }
        return data
    }

}

const mutations = {
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    set_main: (state, data) => {
        state.main = data;
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    ret_main: (state, { name, data }) => {
        if (state.main[name]) {
            state.main[name] = data;
        }
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    set_resources: (state, data) => {
        state.main.resources = data;
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    set_controls: (state, data) => {
        state.main.controls = data;
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    set_libraries: (state, data) => {
        state.main.libraries = data;
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    set_attributes: (state, data) => {
        state.main.attributes = data;
    },
    /**
     * 设置属性数据
     * @param  {[type]} state [description]
     * @param  {[type]} data  [description]
     * @return {[type]}       [description]
     */
    new_page: (state, data) => {
        state.config = data;
    },
    /**
     * 设置工具数据
     * @param  {[type]} state        状态
     * @param  {[type]} options.name 栏目名称
     * @param  {[type]} options.data 栏目数据
     * @return {[type]}              [description]
     */
    set_controls_data: (state, { name, data }) => {
        if (state.main.controls[name]) {
            state.main.controls[name].data = data;
        }
    },
    /**
     * 设置工具数据
     * @param  {[type]} state        状态
     * @param  {[type]} options.name 栏目名称
     * @param  {[type]} options.data 栏目数据
     * @return {[type]}              [description]
     */
    set_resources_data: (state, { name, data }) => {
        if (state.main.resources[0].data[name]) {
            state.main.resources[0].data[name] = data;
        }
    }
}

const actions = {
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setMain({ commit }, data) {
        commit('set_main', data)
    },
    /**
     * 重置属性数据
     * @param  {[type]} options.commit [description]
     * @param  {[type]} data           [description]
     * @return {[type]}                [description]
     */
    retMain({ commit }, data) {
        for (let i in data) {
            if (i !== 'resources' || i !== 'controls') {
                commit('ret_main', {
                    name: i,
                    data: data[i]
                })
            }
        }
    },
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setResources({ commit }, data) {
        commit('set_resources', data)
    },
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setControls({ commit }, data) {
        commit('set_controls', data)
    },
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setLibraries({ commit }, data) {
        commit('set_libraries', data)
    },
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setAttributes({ commit }, data) {
        commit('set_attributes', data)
    },
    /**
     * 设置属性数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setPage({ commit }, data) {
        commit('new_page', data)
    },
    /**
     * 设置工具数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setControlsData({ commit }, data) {
        for (let i in data) {
            let item = data[i];
            commit('set_controls_data', {
                name: i,
                data: item
            })
        }
    },
    /**
     * 设置工具数据
     * @param {[type]} options.commit [description]
     * @param {[type]} data           [description]
     */
    setResourcesData({ commit }, { name, data }) {
        commit('set_resources_data', {
            name: name,
            data: data
        })
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}